import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
// 配置插件去掉加载圆圈
Nprogress.configure({
  showSpinner: false
})

const router = createRouter({
  // 通过函数方法
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/Login/index.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/order/pay',
      component: () => import('@/views/Order/OrderPay.vue'),
      meta: { title: '药品支付' }
    },
    {
      path: '/order/pay/result',
      component: () => import('@/views/Order/OrderPayResult.vue'),
      meta: { title: '药品支付结果' }
    },
    {
      path: '/order/:id',
      component: () => import('@/views/Order/OrderDetail.vue'),
      meta: { title: '药品订单详情' }
    },
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/Order/OrderLogistics.vue'),
      meta: { title: '物流详情' }
    },
    {
      path: '/test',
      component: () => import('@/views/test/Father.vue')
    },
    {
      path: '/user/patient',
      component: () => import('@/views/User/PatientPage.vue'),
      meta: { title: '家庭档案' }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/Consult/ConsultFast.vue'),
      meta: { title: '极速问诊' }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/Consult/ConsultDep.vue'),
      meta: { title: '选择科室' }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/Consult/ConsultIllness.vue'),
      meta: { title: '病情描述' }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/Consult/ConsultPay.vue'),
      meta: { title: '问诊支付' }
    },
    {
      path: '/room',
      component: () => import('@/views/Room/index.vue'),
      meta: { title: '问诊室' },
      beforeEnter(to) {
        // 判断页面参数 payResult 值为false ===> 指定去某个页面
        // to === route
        if (to.query.payResult === 'false') return '/user/room'
      }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/User/ConsultPage.vue'),
      meta: { title: '问诊记录' }
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/User/ConsultDetail.vue'),
      meta: { title: '问诊详情' }
    },
    {
      path: '/',
      component: () => import('@/views/Layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: () => import('@/views/Home/index.vue'),
          meta: {
            title: '首页'
          }
        },
        {
          path: '/article',
          component: () => import('@/views/Article/index.vue'),
          meta: {
            title: '健康医疗'
          }
        },
        {
          path: '/notify',
          component: () => import('@/views/Notify/index.vue'),
          meta: {
            title: '消息中心'
          }
        },
        {
          path: '/user',
          component: () => import('@/views/User/index.vue'),
          meta: {
            title: '我的'
          }
        }
      ]
    }
  ]
})
// 设置路由前置导航守卫没有token需强制到login
router.beforeEach((to) => {
  // 前置路由开启
  Nprogress.start()
  const whiteList = ['/login', '/register']
  const store = useUserStore()
  // 没有token&&访问to的页面不在白名单内
  if (!store.user?.token && !whiteList.includes(to.path)) return '/login'
  // return true  可以不写，默认就是这个
})

router.afterEach((to) => {
  document.title = `xu优医问诊-${to.meta.title}`

  Nprogress.done()
})
export default router

/*
Vue3中的变化
- 使用了两个函数:
  - createRouter() 创建一个路由实例  等价于 new VueRouter({})
    - history: 路由的模式 hash/history 
      - createWebHistory() --> history
      - createWebHashHistory() ---> hash
        - 方法中有一个形参: base?
          - import.meta.env.BASE_URL 获取项目的根路径

- 为什么要获取项目的根路径?
  - 在实例的生产中, 一个域名(ip)下可能会部署多个项目   
    - 电商公司 https://www.ds.com
      - 商城的后台管理系统 https://www.ds.com/manager/...
      - 商城的h5端      https://www.ds.com/h5  
    - 设置项目的基础地址
      在vite.config.ts中配置 base: '根路径(基础地址)'
      可以通过import.meta.env.BASE_URL获取你配置在base中的根路径

 -------------------------
      
Vue2中配置路由:

import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

export default new VueRouter({
  mode: 'hash', // 'history'
  routes: [
     {
      path: '/home',
      component: ()=>import('.../home.vue')
     }
  ]
})
*/
